//
//  DefaultService.swift
//  网络API
//
//  Created by jin on 2024/8/22.
//

import Foundation

//导入网络框架
import Moya

enum DefaultService{
    case ads(position:Int)
    
    case sheets(size:Int)
    case sheetDetail(data:String)
    
    case songs
    case songDetail(data:String)
    
    case userDetail(data:String,nickname:String?)
    
    case register(data:User)
    case login(data:User)
    case resetPassword(data:User)
    
    case sendCode(style:Int,data:CodeRequest)
    case checkCode(data:CodeRequest)
}

// MARK: - 实现TargetType协议
extension DefaultService : TargetType{
    
    /// 返回网址
    var baseURL: URL {
        return URL(string: Config.ENDPOINT)!
    }
    
    /// 返回每个请求的路径
    var path: String {
        switch(self){
        case .ads(_):
             return "v1/ads"
        case .sheets:
             return "v1/sheets"
        case .sheetDetail(let data):
             return "v1/sheets/\(data)"
        case .songs:
             return "v1/songs"
        case .songDetail(let data):
             return "v1/songs/\(data)"
        case .register:
             return "v1/users"
        case .login:
            return "v1/sessions"
        case .resetPassword:
            return "v1/users/reset_password"
        case .userDetail(let data,_):
            return "v1/users/\(data)"
            
        case .sendCode:
            return "v1/codes"
        case .checkCode(_):
            return "v1/codes/check"
        default:
             fatalError("DefaultService path is null")
        }
    }
    
    /// 请求方式
    var method: Moya.Method {
        switch(self){
        case .register, .login, .sendCode, .checkCode, .resetPassword:
            return .post
        default:
            return .get
        }
    }
    
    /// 请求的参数
    var task: Moya.Task {
        switch(self){
        case .ads(let position):
            return ParamUtil.urlRequestParamters(["position":position])
        case .sheets(let size):
            return ParamUtil.urlRequestParamters(["size":size])
            
        case .login(let data):
            return .requestData(data.toJSONString()!.data(using: .utf8)!)
        case .register(let data):
            return .requestData(data.toJSONString()!.data(using: .utf8)!)
        case .resetPassword(let data):
            return .requestData(data.toJSONString()!.data(using: .utf8)!)
            
        case .userDetail(_,let nickname):
            var param:[String:Any]=[:]
            
            if let nickname = nickname {
                param["nickname"]=nickname
            }
            return ParamUtil.urlRequestParamters(param)
            
        case .sendCode(let style, let data):
            return .requestCompositeParameters(bodyParameters: ["phone":data.phone,"email":data.email], bodyEncoding: JSONEncoding.default, urlParameters: ["style":style])
        case .checkCode(let data):
            return .requestData(data.toJSONString()!.data(using: .utf8)!)
            
        default:
            //不传递任何参数
            return .requestPlain
        }
    }
    
    /// 请求头
    var headers: [String : String]? {
        var headers:Dictionary<String,String> = [:]
        
        //内容的类型
        headers["Content-Type"]="application/json"
        
        return headers
    }
    
    
}
